#include "i2c.h"

I2C::I2C(qint8 i2cBusNr) {
  this->i2cBusNr = i2cBusNr;
}

qint8 I2C::writeI2CDevice(quint8 devAddr, quint8 address, quint8 length, qint8 *data){
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
    qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
    return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  char buffer[length+1];
  buffer[0] = address;

  for (quint8 i=0;i<length;i++)
    buffer[i+1] = data[i];

  if ( write(file, buffer, length+1) != 2) {
    qDebug() << "I2C: Failure to write values to I2C Device address";
    return(3);
  }

  close(file);
  return 0;
}

qint8 I2C::writeI2CDevice(quint8 devAddr, quint8 address, quint8 value){
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
    qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
    return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  char buffer[2];
  buffer[0] = address;
  buffer[1] = value;

  if ( write(file, buffer, 2) != 2) {
    qDebug() << "I2C: Failure to write values to I2C Device address";
    return(3);
  }

  close(file);
  return 0;
}

qint8 I2C::writeI2CDevice(quint8 devAddr, quint8 address) {
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
    qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
    return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  if ( write(file, &address, 1) != 2) {
    qDebug() << "I2C: Failure to write values to I2C Device address";
    return(3);
  }

  close(file);
  return 0;
}

qint8 I2C::readI2CDevice(quint8 devAddr, qint8 *data) {
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
         qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
         return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  int bytesRead = read(file, data, 1);
  if (bytesRead == -1){
    qDebug() << "I2C: Failed to read";

    close(file);
    return(3);
  }
  close(file);

  return 0;
}

qint8 I2C::readI2CDevice(quint8 devAddr, quint8 address, qint8 *data) {
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
         qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
         return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  char buf[1] = { address };
  if(write(file, buf, 1) !=1){
    qDebug() << "I2C: Failed to set I2C ";
  }

  int bytesRead = read(file, data, 1);
  if (bytesRead == -1){
    qDebug() << "I2C: Failed to read";
  }
  close(file);

  return 0;
}

qint8 I2C::readI2CDevice(quint8 devAddr, quint8 address, quint8 length, qint8 *data) {
  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBusNr);

  int file;
  if ((file = open(namebuf, O_RDWR)) < 0){
         qDebug() << "I2C: Failed to open " << namebuf << " I2C Bus";
         return(1);
  }
  if (ioctl(file, I2C_SLAVE, devAddr) < 0){
    qDebug() << "I2C: SLAVE address" << devAddr << "failed...";
    return(2);
  }

  char buf[1] = { address };
  if(write(file, buf, 1) !=1){
    qDebug() << "I2C: Failed to set I2C ";
  }

  int bytesRead = read(file, data, length);
  if (bytesRead == -1){
    qDebug() << "I2C: Failed to read";
  }
  close(file);

  return 0;
}
